package cn.iocoder.yudao.module.system.dal.mysql.chaoshun;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.framework.common.enums.CommonSfEnum;
import cn.iocoder.yudao.framework.common.enums.CommonSpztEnum;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.system.controller.admin.chaoshun.vo.sp.SpPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.chaoshun.vo.sp.YhxmVO;
import cn.iocoder.yudao.module.system.dal.dataobject.chaoshun.ChaoshunSpDO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;
import java.util.stream.Collectors;


/**
* @author 16651
* @description 针对表【chaoshun_sp】的数据库操作Mapper
* @createDate 2024-04-14 22:51:35
* @Entity cn.iocoder.yudao.module.system.dal.dataobject.chaoshun.ChaoshunSp
*/
@Mapper
public interface ChaoshunSpMapper extends BaseMapperX<ChaoshunSpDO> {

    default PageResult<ChaoshunSpDO> selectPage(SpPageReqVO reqVO) {
        String type = reqVO.getType();
        Integer spzt = reqVO.getSpzt();
        LambdaQueryWrapper<ChaoshunSpDO> queryWrapper = new LambdaQueryWrapperX<ChaoshunSpDO>()
                .likeIfPresent(ChaoshunSpDO::getSpmc, reqVO.getSpmc())
                .likeIfPresent(ChaoshunSpDO::getShdh, reqVO.getShdh())
                .likeIfPresent(ChaoshunSpDO::getShr2, reqVO.getShr2())
                .likeIfPresent(ChaoshunSpDO::getFhdh, reqVO.getFhdh())
                .likeIfPresent(ChaoshunSpDO::getFhr, reqVO.getFhr())
                .eqIfPresent(ChaoshunSpDO::getStatus, reqVO.getStatus())
                .eqIfPresent(ChaoshunSpDO::getSffh, reqVO.getSffh())
                .gtIfPresent(CommonSfEnum.isEnable(reqVO.getSfyc()), ChaoshunSpDO::getYccs, 0)
                .eqIfPresent(CommonSfEnum.isDisable(reqVO.getSfyc()), ChaoshunSpDO::getYccs, 0)
                .betweenIfPresent(ChaoshunSpDO::getLrsj, reqVO.getLrsj())
                .betweenIfPresent(ChaoshunSpDO::getApshsj, reqVO.getApshsj())
                .eqIfPresent(ChaoshunSpDO::getSonghr, reqVO.getSonghr());
        // 收发货页面查询spzt
        if ("courier".equals(type)) {
            if (ObjectUtil.isNull(spzt)) {
                ((LambdaQueryWrapperX<ChaoshunSpDO>) queryWrapper).betweenIfPresent(ChaoshunSpDO::getSpzt,
                        new Integer[]{CommonSpztEnum.KDDFH.getStatus(), CommonSpztEnum.KDYSH.getStatus()});
            } else {
                ((LambdaQueryWrapperX<ChaoshunSpDO>) queryWrapper).eqIfPresent(ChaoshunSpDO::getSpzt, reqVO.getSpzt());
            }
        } else if ("info".equals(type) && ObjectUtil.isNull(spzt)) {
            ((LambdaQueryWrapperX<ChaoshunSpDO>) queryWrapper).leIfPresent(ChaoshunSpDO::getSpzt, CommonSpztEnum.YQH.getStatus());
        }
        queryWrapper.and(StrUtil.isNotEmpty(reqVO.getJqShdh()) || StrUtil.isNotEmpty(reqVO.getJqFhdh()),
                wrappers -> wrappers.eq(StrUtil.isNotEmpty(reqVO.getJqShdh()), ChaoshunSpDO::getShdh, reqVO.getJqShdh())
                        .or().eq(StrUtil.isNotEmpty(reqVO.getJqFhdh()), ChaoshunSpDO::getFhdh, reqVO.getJqFhdh()));
        return selectPage(reqVO, queryWrapper
                .orderByDesc(!CommonSfEnum.isEnable(reqVO.getSfdc()), ChaoshunSpDO::getId)
                .orderByAsc(CommonSfEnum.isEnable(reqVO.getSfdc()), ChaoshunSpDO::getShr2));
    }

    default List<ChaoshunSpDO> getYhxxByXm(YhxmVO yhxmVO) {
        boolean sfFhr = StrUtil.isNotEmpty(yhxmVO.getFhr());
        LambdaQueryWrapperX<ChaoshunSpDO> wrapper = new LambdaQueryWrapperX<ChaoshunSpDO>()
                .eqIfPresent(ChaoshunSpDO::getFhr, yhxmVO.getFhr())
                .eqIfPresent(ChaoshunSpDO::getShr2, yhxmVO.getShr2());
        if (sfFhr) {
            wrapper.select(ChaoshunSpDO::getFhr,ChaoshunSpDO::getFhdh);
        } else {
            wrapper.select(ChaoshunSpDO::getShr2,ChaoshunSpDO::getShdh,ChaoshunSpDO::getSpmc);
        }
        return selectPage(yhxmVO, wrapper
                .orderByAsc(ChaoshunSpDO::getId)).getList().stream().distinct().collect(Collectors.toList());
    }

    default PageResult<ChaoshunSpDO> selectDyPage(SpPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<ChaoshunSpDO>()
                .eqIfPresent(ChaoshunSpDO::getSfdypz, "0")
                .orderByAsc(ChaoshunSpDO::getId));
    }

    default ChaoshunSpDO selectBySpmc(String spmc) {
        return selectOne(ChaoshunSpDO::getSpmc, spmc);
    }

}




